<template>
  <el-breadcrumb class="breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item
        v-for="(item, index) in breadcrumbData"
        :key="item.path"
      >
        <!-- 不能点击 -->
        <span v-if="index === breadcrumbData.length - 1" class="no-redirect">{{
          gererateTitle(item.meta.title)
        }}</span>
        <!-- //可以点击 -->
        <a v-else class="redirect" @click.prevent="onLinkClick(item)">{{
          gererateTitle(item.meta.title)
        }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script setup>
import { ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { gererateTitle } from '@/utils/i18n'
const route = useRoute()

const breadcrumbData = ref([])
const getBreadcrumbData = () => {
  breadcrumbData.value = route.matched.filter(
    (item) => item.meta && item.meta.title
  )
}

const router = useRouter()
const onLinkClick = (item) => {
  router.push(item.path)
}
watch(
  route,
  () => {
    getBreadcrumbData()
  },
  {
    immediate: true
  }
)
</script>

<style lang="scss" scoped>
.breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 50px;
  margin-left: 10px;
}
::v-deep no-redirect {
  color: #97a8be;
  cursor: text;
}
::v-deep .redirect {
  color: #666;
  cursor: pointer;
}
</style>
